﻿#include "wsignallampnode.h"
#include "wnodemanager.h"
#include "wxpolygonex.h"
#include "../common.h"
#include "../scriptmanager.h"
#include <wx/strconv.h>

WSignalLampNode::WSignalLampNode()
    : m_NameAlignment(TextAlignment::LEFT),
      m_SLRouteType(SLRouteType::ROUTE_NULL)
{
    this->setNodeType(NODE_SIGNAL);
}

WSignalLampNode::WSignalLampNode(const wxString& pname)
    : WBaseNode(pname),
      m_NameAlignment(TextAlignment::LEFT),
      m_SLRouteType(SLRouteType::ROUTE_NULL)
{
    this->setNodeType(NODE_SIGNAL);
}

WSignalLampNode::~WSignalLampNode()
{

}

/// 建立脚本文件
bool WSignalLampNode::createScriptFile(void)
{
    // 目标文件
    /*const wxString decScriptFile = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath()+
                             wxString::Format("\\resources\\scripts\\nodes\\%s.lua",this->getName());

    if(wxFileExists(decScriptFile))
    {
        /// 注册结点到lua中
        ScriptManager::getSingleton().registerBaseNodetoLua("L_",this);

        return false;
    }

    const wxString decScriptDir = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath()+
            "\\resources\\scripts\\nodes";

    // 检测指定目录是否存在，不存在创建目录
    if(!wxDir::Exists(decScriptDir))
    {
        if (!wxFileName::Mkdir(decScriptDir, 0755, wxPATH_MKDIR_FULL))
            return false;
    }

    wxString tmpScriptContent = wxString(
                                         "%s_radius = 1.0;\n"
                                         "\n"
                                         "--结点更新\n"
                                         "function update_%s()\n"
                                         "\n"
                                         "end\n"
                                         "\n"
                                         "--[[鼠标事件处理]]--\n"
                                         "function onMouseEvent_%s(event)\n"
                                         "  local p%s = WSignalLampNode.castFromBase(%s);\n"
                                         "\n"
                                         "  if p%s:getBoundingRect():containsPoint(event:GetPosition()) then\n"
                                         "      -- 左键按下，信号灯必须在正常状态才能选中信号灯\n"
                                         "      if event:ButtonDown(1) == true and p%s:getOperState() == 2 then\n"
                                         "          p%s:setOperState(0);\n"
                                         "      end\n"
                                         "\n"
                                         "      %s_radius = 1.2;\n"
                                         "  else\n"
                                         "      -- 右键抬起，取消选中信号灯\n"
                                         "      if event:ButtonDown(3) == true and p%s:getOperState() == 0 then\n"
                                         "          p%s:setOperState(2);\n"
                                         "      end\n"
                                         "\n"
                                         "      %s_radius = 1.0;\n"
                                         "  end\n"
                                         "end\n"
                                         "\n"
                                         "--绘制结点\n"
                                         "function draw_%s(painter)\n"
                                         "   local p%s = WSignalLampNode.castFromBase(%s);\n"
                                         "\n"
                                         "   local tmpPos = p%s:getScreenPosition();\n"
                                         "   painter:DrawCircle(tmpPos,10,17,\n"
                                         "                  %s:getColourByState(),\n"
                                         "                  wxColor(255,255,255,255),\n"
                                         "                  3.0,\n"
                                         "                  %s_radius);\n"
                                         "\n"
                                         "   --绘制结点名称\n"
                                         "   p%s:drawName(painter);\n"
                                         "end");
    tmpScriptContent.Replace("%s","L_"+this->getName());


    wxFile decfile;
    if (!decfile.Create(decScriptFile, true))
        return false;

    wxCSConv conv("GB2312");

    if (!decfile.Write(tmpScriptContent,conv)) {
        decfile.Close();
        return false;
    }

    decfile.Close();*/

    /// 注册结点到lua中
    ScriptManager::getSingleton().registerBaseNodetoLua("L_",this);

    return true;
}

/// 根据状态得到颜色
wxColour WSignalLampNode::getColourByState(void)
{
    wxColour decColour = wxColour(255,255,255,255);

    // 信号条件
    switch(this->getSignalState())
    {
    case NodeSignalState::OCCUPY:             // 占用
    {
        switch(this->getNodeType())
        {
        case NodeType::NODE_SWITCH:
            decColour = wxColour(255, 0, 0,255);
            break;
        case NodeType::NODE_SIGNAL:
            decColour = wxColour(255, 255, 255,255);
            break;
        default:
            break;
        }
    }
    break;
    case NodeSignalState::CLEAR:              // 蓝灯
    {
        switch(this->getNodeType())
        {
        case NodeType::NODE_SWITCH:
            decColour = wxColour(255, 255, 255,255);
            break;
        case NodeType::NODE_SIGNAL:
            decColour = wxColour(0, 0, 255,255);
            break;
        default:
            break;
        }
    }
    break;
    case NodeSignalState::NOSIGNAL:           // 没信号
    {
        decColour = wxColour(255, 255, 0,255);

        switch(this->getNodeType())
        {
        case NodeType::NODE_SWITCH:
            decColour = wxColour(255, 255, 0,255);
            break;
        case NodeType::NODE_SIGNAL:
            decColour = wxColour(0, 0, 255,255);
            break;
        default:
            break;
        }
    }
    break;
    default:
        break;
    }

    return decColour;
}

/// 工作区域改变
void WSignalLampNode::changeWorkingRect(wxRect workingrect)
{
    WBaseNode::changeWorkingRect(workingrect);

    // 计算实际显示点
    mScreenCenterPosition = wxRealPoint(workingrect.GetLeftTop().x+workingrect.GetWidth()*(mCenterPosition.x/100.0f),
                              workingrect.GetLeftTop().y+workingrect.GetHeight()*(mCenterPosition.y/100.0f));

    // 计算包围盒
    wxPoint points[4];
    points[0] = wxPoint(mScreenCenterPosition.x-IDD_SIGNALLAMP_RADIUS,mScreenCenterPosition.y-IDD_SIGNALLAMP_RADIUS);
    points[1] = wxPoint(mScreenCenterPosition.x+IDD_SIGNALLAMP_RADIUS,mScreenCenterPosition.y-IDD_SIGNALLAMP_RADIUS);
    points[2] = wxPoint(mScreenCenterPosition.x+IDD_SIGNALLAMP_RADIUS,mScreenCenterPosition.y+IDD_SIGNALLAMP_RADIUS);
    points[3] = wxPoint(mScreenCenterPosition.x-IDD_SIGNALLAMP_RADIUS,mScreenCenterPosition.y+IDD_SIGNALLAMP_RADIUS);

    this->setBoundingRect(wxPolygonEx(4,points));
}

/// 处理鼠标事件
bool WSignalLampNode::OnMMouseEvent(wxMouseEvent& event)
{
    /// 调用结点的鼠标事件接口
    ScriptManager::getSingleton().callLuaFunctionWithBaseNodeMouseEvent("L_"+this->getName(),
                                                                        event);

    return false;
}

/// 更新
void WSignalLampNode::update(void)
{
    // 调用lua更新脚本接口
    ScriptManager::getSingleton().callLuaFunctionWithBaseNodeUpdate("L_"+this->getName());
}
